package com.sys.core.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by 汤伟杰 on 2017/6/8.
 */
public class HttpConnectionXML {

    public static String sendPost(String urlStr,String xmlInfo) throws Exception {
        try {
            URL url = new URL(urlStr);
            URLConnection con = url.openConnection();
            con.setDoOutput(true);
            con.setRequestProperty("Pragma", "no-cache");
            con.setRequestProperty("Cache-Control", "no-cache");
            con.setRequestProperty("Content-Type", "text/xml");

            OutputStreamWriter out = new OutputStreamWriter(con
                    .getOutputStream());
            System.out.println("post:"+urlStr+"\n"+xmlInfo);
            out.write(new String(xmlInfo.getBytes("UTF-8")));
            out.flush();
            out.close();
            BufferedReader br = new BufferedReader(new InputStreamReader(con
                    .getInputStream()));
            String line = "";
            System.out.println("返回结果：");
            StringBuffer xml = new StringBuffer();
            for (line = br.readLine(); line != null; line = br.readLine()) {
                xml.append(line);
                new String(xml.toString().getBytes("GB2312"), "UTF-8");
            }
            String sxml = new String(xml.toString().getBytes("UTF-8"), "ISO-8859-1");
            System.out.println(sxml);
            return sxml;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
